package com.biz.service.basic;


import com.biz.model.Hmodel.Tmebactivity;
import com.biz.model.Hmodel.basic.TBaseMebactivityApprove;
import com.biz.model.Pmodel.PBaseUser;
import com.biz.model.Pmodel.Pmebactivity;
import com.biz.service.base.BaseServiceImpl;
import com.framework.dao.hdao.BaseDaoI;
import com.framework.dao.mdao.DaoSupport;
import com.framework.model.Pager;
import com.framework.model.Paging;
import com.framework.model.Params;
import com.framework.utils.BeanUtils;
import com.framework.utils.StringUtil;
import com.framework.utils.UuidUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by liujiajia on 2017/1/6.
 */
@Service("mebactivityService")
public class MebactivityServiceImpl extends BaseServiceImpl<Tmebactivity> implements MebactivityServiceI {

    @Resource(name = "daoSupport")
    private DaoSupport dao;
    @Autowired
    private BaseDaoI<Tmebactivity> mebactivityDao;

    @Autowired
    private BaseDaoI<TBaseMebactivityApprove> tBaseMebactivityApproveDao;


    @Override
    public Paging findMebactivityGrid(Params sqlParams) throws Exception {
        Paging paging= dao.findForPagings("mebactivityMapper.findMebactivityGrid",sqlParams,"mebactivityMapper.findMebactivityCount",sqlParams);
        return paging;
    }

    @Override
    public void delGridById(String ids) {

            String [] idList=ids.split(",");
            for (String id:idList) {
                Tmebactivity mebactivity= mebactivityDao.getById(Tmebactivity.class,id);
                mebactivity.setIsdel(1);
                mebactivityDao.update(mebactivity);
            }

    }

    @Override
    public Pmebactivity findById(String id) throws Exception {
        return (Pmebactivity) dao.findForObject("mebactivityMapper.findMebactivityById",id);
    }

    @Override
    public void saveMebactivity(Pmebactivity pmebactivity) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Tmebactivity tmebactivity=new Tmebactivity();
        BeanUtils.copyNotNullProperties(pmebactivity, tmebactivity,new String[]{"applyStartTime","applyEndTime","createTime"});
        tmebactivity.setId(UuidUtil.get32UUID());
        tmebactivity.setState(0);
        tmebactivity.setApplyStartTime(sdf.parse(pmebactivity.getApplyStartTime()));
        tmebactivity.setApplyEndTime(sdf.parse(pmebactivity.getApplyEndTime()));
        tmebactivity.setIsdel(0);
        mebactivityDao.save(tmebactivity);
    }

    @Override
    public void updateMebactivity(Pmebactivity pmebactivity) throws Exception{
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Tmebactivity tmebactivity=mebactivityDao.getById(Tmebactivity.class,pmebactivity.getId());
        BeanUtils.copyNotNullProperties(pmebactivity,tmebactivity,new String[]{"applyStartTime","applyEndTime","createTime"});
        tmebactivity.setApplyStartTime(sdf.parse(pmebactivity.getApplyStartTime()));
        tmebactivity.setApplyEndTime(sdf.parse(pmebactivity.getApplyEndTime()));
        tmebactivity.setState(0);
        mebactivityDao.update(tmebactivity);
    }

    @Override
    public String findLastDate() throws Exception {
        return (String)dao.findForObject("mebactivityMapper.findLastDate",null);
    }
    @Override
    public String findPeriod() throws Exception {
        return (String)dao.findForObject("mebactivityMapper.findPeriod",null);
    }

    @Override
    public Paging showMebActivityPerson(Map map) throws Exception {
        //参数
        Pager<Map<String, Object>> pager = calculateAndTransformByMap(map);
        //查询
        Pager<Map<String, Object>> pager_back = (Pager<Map<String, Object>>) dao
                .findForPager1("mebactivityMapper.showMebActivityPerson",
                        "mebactivityMapper.showMebActivityPersonCount", pager);
        //封装成适合前台使用的格式
        Paging paging = new Paging<>();
        paging.setRows(pager_back.getExhibitDatas());
        paging.setTotal((long) pager_back.getRecordCount());
        return paging;
    }

    @Override
    public Map<String,Object> changeIsOpen(String id, String isOpen) throws Exception {
        Map<String,Object> map=new HashedMap();
        map.put("id",id);
        map.put("isOpen",isOpen);
        //判断该活动是否有人报名
        int count =(int)dao.findForObject("mebactivityMapper.getCountById",id);
        if(count==0){
            dao.update("mebactivityMapper.changeIsOpen",map);
            map.put("flag","1");
            map.put("msg","操作成功");
            return map;
        }else{
            map.put("flag","0");
            map.put("msg","已有报名的活动不能关闭");
            return map;
        }
    }

    @Override
    public Map<String, Object> approve(String id, String note,String state,String userId) throws Exception {
       Map<String,Object> resMap=new HashedMap();
        //更新主表状态
        Map<String,Object> map=new HashedMap();
        map.put("id",id);
        map.put("note",note);
        map.put("state",state);
        map.put("userId",userId);
        dao.update("mebactivityMapper.updateMebactivity",map);
        //子表添加记录
        TBaseMebactivityApprove tBaseMebactivityApprove=new TBaseMebactivityApprove();
        tBaseMebactivityApprove.setState(Integer.valueOf(map.get("state").toString()));
        tBaseMebactivityApprove.setId(UuidUtil.get32UUID());
        tBaseMebactivityApprove.setApproveTime(new Date());
        tBaseMebactivityApprove.setApproveUserId(userId);
        tBaseMebactivityApprove.setMainId(id);
        tBaseMebactivityApprove.setNotes(note);
        tBaseMebactivityApproveDao.save(tBaseMebactivityApprove);
        resMap.put("flag","1");
        return resMap;
    }

    @Override
    public Paging showMebActivityApprove(Map map) throws Exception {
        //参数
        Pager<Map<String, Object>> pager = calculateAndTransformByMap(map);
        //查询
        Pager<Map<String, Object>> pager_back = (Pager<Map<String, Object>>) dao
                .findForPager1("mebactivityMapper.showMebActivityApprove",
                        "mebactivityMapper.showMebActivityApproveCount", pager);
        //封装成适合前台使用的格式
        Paging paging = new Paging<>();
        paging.setRows(pager_back.getExhibitDatas());
        paging.setTotal((long) pager_back.getRecordCount());
        return paging;
    }

    @Override
    public Paging showApplyDetailList(Map map) throws Exception {
        //参数
        Pager<Map<String, Object>> pager = calculateAndTransformByMap(map);
        //查询
        Pager<Map<String, Object>> pager_back = (Pager<Map<String, Object>>) dao
                .findForPager1("mebactivityMapper.showApplyDetailList",
                        "mebactivityMapper.showApplyDetailListCount", pager);
        //封装成适合前台使用的格式
        Paging paging = new Paging<>();
        paging.setRows(pager_back.getExhibitDatas());
        paging.setTotal((long) pager_back.getRecordCount());
        return paging;
    }

    @Override
    public Map<String, Object> approveApply(String id, String note, String state) throws Exception {
        Map<String,Object> resMap=new HashedMap();
        Map<String,Object> pmap=new HashedMap();
        pmap.put("id",id);
        pmap.put("appNotes",note);
        pmap.put("approvState",state);
        dao.update("mebactivityMapper.approveApply",pmap);
        resMap.put("flag","1");
        return resMap;
    }

    @Override
    public Paging showMebIntegerList(Map map) throws Exception {
        //参数
        Pager<Map<String, Object>> pager = calculateAndTransformByMap(map);
        //查询
        Pager<Map<String, Object>> pager_back = (Pager<Map<String, Object>>) dao
                .findForPager1("mebactivityMapper.showMebIntegerList",
                        "mebactivityMapper.showMebIntegerListCount", pager);
        //封装成适合前台使用的格式
        Paging paging = new Paging<>();
        paging.setRows(pager_back.getExhibitDatas());
        paging.setTotal((long) pager_back.getRecordCount());
        return paging;
    }

    @Override
    public String updateState(String id, String state) throws Exception {
        Map<String,Object> map=new HashedMap();
        map.put("id",id);
        map.put("state",state);
        dao.update("mebactivityMapper.updateState",map);
        return "操作成功";
    }

    @Override
    public String addMebInteger(String id) throws Exception {
        //关闭活动
        dao.update("mebactivityMapper.updateMebState",id);
        //获取增加的积分
        int integer=(Integer) dao.findForObject("mebactivityMapper.findIntegerById",id);
        //获取积分列表
        List<PBaseUser> list=(List<PBaseUser>) dao.findForList("mebactivityMapper.getIntegerList",id);
        String ids="";
        for(int i=0;i<list.size();i++){
            ids+=ids+list.get(i).getUserId()+",";
            list.get(i).setId(UuidUtil.get32UUID());
            list.get(i).setInteger(integer+"");
        }
        Map<String,Object> map=new HashedMap();
        map.put("integer",integer);
        map.put("ids",StringUtil.formatSqlIn(ids));
        if(!StringUtil.isNullOrEmpty(ids)){
            //增加积分
            dao.update("mebactivityMapper.updateInteger",map);
            //添加记录
            dao.save("mebactivityMapper.insertIntegerRecord",list);
        }
//        //增加积分
//        dao.update("mebactivityMapper.updateInteger",map);
//        //添加记录
//        dao.save("mebactivityMapper.insertIntegerRecord",list);
        return "操作成功！";
    }
}
